package com.atguigu.gulimail.order.pay.controller;

import com.alipay.api.AlipayApiException;
import com.atguigu.gulimail.order.entity.OrderEntity;
import com.atguigu.gulimail.order.pay.AliPayBean;
import com.atguigu.gulimail.order.pay.service.PayService;
import com.atguigu.gulimail.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

@Controller
public class PayController {

    @Autowired
    OrderService orderService;

    @Autowired
    PayService payService;

    @ResponseBody
    @GetMapping(value = "/aliPayOrder",produces = "text/html")
    public String aliPayOrder(@RequestParam("orderSn") String orderSn) throws AlipayApiException {
        OrderEntity order = orderService.getByOrderSn(orderSn);

        AliPayBean alipayBean = new AliPayBean();
        alipayBean.setOut_trade_no(order.getOrderSn());//订单号
        alipayBean.setSubject("测试");//订单标题
        BigDecimal bigDecimal = order.getTotalAmount().setScale(2, BigDecimal.ROUND_UP);//设置订单金额保留两位小数
        alipayBean.setTotal_amount(bigDecimal.toString());//订单金额
        alipayBean.setBody(order.getCreateTime().toString());//订单描述

        return payService.aliPay(alipayBean);
    }

}
